/** ===============================================
 *  请求之后数据做一个拦截
 *  ===============================================*/
module.exports = {
	resuleEnum: {
		SUCCESS: 0,
		ERROR: -1,
		TIMEOUT: 401,
		TYPE: 'success'
	},

	// 请求成功
	resultSuccess: function (data, { message = 'ok' } = {}) {
		return {
			code: this.resuleEnum.SUCCESS,
			data,
			message,
			type: 'success'
		}
	},

	// 请求失败
	resultError: function (message = 'Request failed', { code = this.resuleEnum.ERROR, data = null } = {}) {
		return {
			code: this.resuleEnum.ERROR,
			data,
			message,
			type: 'error'
		}
	},

	// 分页查询
	resultPageSuccess: function (
		page,
		pageSize,
		list,
		{ message = 'ok' } = {}
	) {
		const pageData = this.pagination(page, pageSize, list)

		return {
			...this.resultSuccess({
				pageDTO: {
					pageNo: page,
					pageSize: pageSize,
					totalCount: list.length
				},
				records: pageData
			}),
			message
		}
	},

	// 分页截取
	pagination: function (pageNo, pageSize, array) {
		const offset = pageNo * Number(pageSize)

		return offset + Number(pageSize) >= array.length
		  ? array.slice(offset, array.length)
		  : array.slice(offset, offset + Number(pageSize))
	},
}

